package com.itheima.reggie.controller;

import com.github.pagehelper.PageHelper;
import com.itheima.reggie.config.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.service.CategoryService;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired(required = false)
    private CategoryService categoryService;

    /**
     * 添加菜品类型
     */
    //    http://localhost:8080/category
    @PostMapping
    public R save(@RequestBody Category category, HttpSession session) {
//        1.补全创建信息
        Long empId = (Long) session.getAttribute("employee");
        category.setCreateUser(empId);
        category.setUpdateUser(empId);
        categoryService.save(category);
        return R.success("保存成功");
    }

    /**
     * 分页查询
     * http://localhost:8080/category/page?page=1&pageSize=10
     */
    @GetMapping("/page")
    public R pageShow(@RequestParam(defaultValue = "1") Integer page,
                      @RequestParam(defaultValue = "10") Integer pageSize) {

        Page<Category> categoryPage = categoryService.findAll(page, pageSize);
        return R.success(categoryPage);
    }

    /**
     * 删除菜品
     * http://localhost:8080/category?id=1397844303408574465
     */
    @DeleteMapping
    public R deleteCate(long id) {
        categoryService.deleteCate(id);
        return R.success("删除成功");
    }

    /**
     * 修改数据
     * RL: http://localhost:8080/category
     * put
     */
    @PutMapping
    public R updateCate(@RequestBody Category category, HttpSession session) {
//            添加修改时间
        Long empId = (Long) session.getAttribute("employee");
        category.setUpdateUser(empId);
        category.setUpdateTime(LocalDateTime.now());
        categoryService.update(category);
        return R.success("更新成功");
    }

    /*
     http://localhost:8080/category/exportExcel
     */
    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        InputStream resourceAsStream = this.getClass().getResourceAsStream("/excel/cateory.xlsx");
//        创建工作簿
        Workbook workbook = new XSSFWorkbook(resourceAsStream);
//        获取工作单
        Sheet sheetAt = workbook.getSheetAt(0);
        //        获取一行数据
        Row row = sheetAt.getRow(1);
//        创建一个集合村存放风格
        List<CellStyle> cellStyleList = new ArrayList<>();
        for (int i = 1; i <= 5; i++) {
//            获取每一个单元格的风格
            CellStyle cellStyle = row.getCell(i).getCellStyle();
            cellStyleList.add(cellStyle);
        }
//        查询页面数据
        List<Category> allCategory = categoryService.findAllmsg();
//        创建一行
        for (Category category : allCategory) {
            row = sheetAt.createRow(1);
//            第一行
            Cell cell = row.createCell(1);
            cell.setCellStyle(cellStyleList.get(0));
            cell.setCellValue(category.getType());
            //            第二行
            cell = row.createCell(2);
            cell.setCellStyle(cellStyleList.get(1));
            cell.setCellValue(category.getName());
            //            第三行
            cell = row.createCell(3);
            cell.setCellStyle(cellStyleList.get(2));
            cell.setCellValue(category.getSort());
            //            第四行
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            cell = row.createCell(4);
            cell.setCellStyle(cellStyleList.get(3));
            cell.setCellValue(dateTimeFormatter.format(category.getCreateTime()));
            //            第5行
            cell = row.createCell(5);
            cell.setCellStyle(cellStyleList.get(4));
            cell.setCellValue(dateTimeFormatter.format(category.getUpdateTime()));

            response.setHeader("content-disposition", "attachment;filename=category.xlsx");
            workbook.write(response.getOutputStream());

        }

    }

    /**
     * 获取菜品分类
     * http://localhost:8080/category/list?type=1
     * get
     */
    @GetMapping("/list")
    public R getList(Integer type) {
       List<Category>list =categoryService.findByType(type);
       return R.success(list);
    }
}
